| 1 |  |  | import { parse } from 'jsonplus'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import { readFileSync, existsSync } from 'fs'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import { join as pathJoin } from 'path'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | export interface SyncConfig { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |   username?: string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |   password?: string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |   port?: number; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |    * Host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |   host: string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |    * Local path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |   localPath: string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |    * Remote path target | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |   remotePath: string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |    * SSH private key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |   privateKey?: string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |   ignores?: Array<string | RegExp>; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |   pathMode?: string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | export enum EXIT_CODE { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |    * Exit normally | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |   NORMAL = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |    * Any kind exit with error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |   RUNTIME_FAILURE = 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |    * If user terminates with ctrl-c use this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |   TERMINATED = 130, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |    * Tell user that arguments were wrong | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |   INVALID_ARGUMENT = 128, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  | export const CONFIG_FILE_NAME = 'sync-config.json'; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | export default class Config implements SyncConfig { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |   username?: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |   password?: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |   port?: number; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |   host: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |   localPath: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |   remotePath: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |   privateKey?: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |   ignores?: (string | RegExp)[]; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |   pathMode?: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |   private _filename: string; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |   private _config: SyncConfig; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |   constructor() { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     this._filename = pathJoin(process.cwd(), CONFIG_FILE_NAME); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |   ready(): Promise<void> { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     return new Promise<void>((resolve, reject) => { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |       this._fetch(); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |       this._expand(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |       var ignores = [/node\_modules/, /vendor/, /\.git/]; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |       if (this.ignores.length) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         this.ignores = this.ignores.concat(ignores); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |       } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         this.ignores = ignores; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |       } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |       // Temporary | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |       if (!this.password) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         reject('Password required'); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         throw 'Password required'; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         //resolve(); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |       } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         resolve(); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |       } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     }); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |   private _fetch() { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     if (existsSync(this._filename)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |       let configraw: Buffer; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |       if ((configraw = readFileSync(this._filename))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |           this._config = parse(configraw.toString()); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } catch (e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |           console.log( | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             'Could not parse DB file. Make sure JSON is correct', | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             EXIT_CODE.RUNTIME_FAILURE | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |           ); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |       } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         console.log( | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |           'Cannot read config file. Make sure you have permissions', | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |           EXIT_CODE.INVALID_ARGUMENT | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |       } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |       console.log('Config file not found', EXIT_CODE.INVALID_ARGUMENT); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |   /** | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |    * @TODO add checks on required values | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |   private _expand() { | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     [ | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |       'host', | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |       'port', | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |       'username', | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |       'password', | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |       'pathMode', | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |       'localPath', | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |       'remotePath', | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |       'ignores', | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |       'privateKey', | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |     ].forEach((prop) => { | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |       this[prop] = this._config[prop] || this[prop]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |   } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  |